package org.feng.fxfront.pages.animal;

import javafx.geometry.Insets;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.feng.api.DateRange;
import org.feng.api.animal.constant.AnimalSexEnum;
import org.feng.api.animal.constant.AnimalTypeEnum;
import org.feng.api.animal.request.QueryAnimalRequest;
import org.feng.fxfront.pages.components.ComboBoxBaseEnumConvert;
import org.feng.fxfront.util.ControlUtil;
import org.feng.fxfront.util.StringUtil;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.IntStream;

/**
 * 搜索动物面板
 *
 * @author feng
 */
@Data
@Slf4j
public class SearchAnimalPane {

    // 折叠面板
    private TitledPane searchPane;

    // 搜索名字文本框
    private TextField searchNameTextField;
    // 搜索年龄下拉框
    private ComboBox<Integer> searchAgeComboBox;
    // 搜索动物类型下拉框
    private ComboBox<Integer> searchTypeComboBox;
    // 搜索动物性别下拉框
    private ComboBox<Integer> searchSexComboBox;
    // 单选按钮组-性别
    private ToggleGroup sexGroup;
    // 创建日期始
    private DatePicker createDateStart;
    // 创建日期止
    private DatePicker createDateEnd;
    // 更新日期始
    private DatePicker updateDateStart;
    // 更新日期止
    private DatePicker updateDateEnd;
    // 查询按钮，触发查询
    private Button searchButton;

    public SearchAnimalPane(double width) {
        // 定义搜索网格面板
        GridPane searchGridPane = new GridPane();
        // 设置垂直间距，水平间距
        searchGridPane.setVgap(10);
        searchGridPane.setHgap(5);
        searchNameTextField = new TextField();

        // 下拉框
        searchTypeComboBox = new ComboBox<>();
        searchTypeComboBox.getItems().addAll(Arrays.stream(AnimalTypeEnum.values()).map(AnimalTypeEnum::getCode).toList());
        searchSexComboBox = new ComboBox<>();
        searchSexComboBox.getItems().addAll(Arrays.stream(AnimalSexEnum.values()).map(AnimalSexEnum::getCode).toList());
        searchTypeComboBox.setConverter(new ComboBoxBaseEnumConvert(AnimalTypeEnum.class));
        searchSexComboBox.setConverter(new ComboBoxBaseEnumConvert(AnimalSexEnum.class));
        searchAgeComboBox = new ComboBox<>();
        searchAgeComboBox.getItems().addAll(IntStream.range(0, 100).boxed().toList());

        // 单选按钮组
        sexGroup = new ToggleGroup();
        RadioButton unknownButton = new RadioButton(AnimalSexEnum.UN_KNOWN.getDesc());
        unknownButton.setToggleGroup(sexGroup);
        unknownButton.setUserData(AnimalSexEnum.UN_KNOWN.getCode());
        RadioButton maleButton = new RadioButton(AnimalSexEnum.MALE.getDesc());
        maleButton.setToggleGroup(sexGroup);
        maleButton.setUserData(AnimalSexEnum.MALE.getCode());
        RadioButton femaleButton = new RadioButton(AnimalSexEnum.FEMALE.getDesc());
        femaleButton.setToggleGroup(sexGroup);
        femaleButton.setUserData(AnimalSexEnum.FEMALE.getCode());

        searchButton = ControlUtil.genSearchButton();
        Button clearButton = ControlUtil.genClearButton();
        createDateStart = new DatePicker();
        createDateEnd = new DatePicker();
        updateDateStart = new DatePicker();
        updateDateEnd = new DatePicker();
        createDateStart.setEditable(true);
        createDateEnd.setEditable(true);
        updateDateStart.setEditable(true);
        updateDateEnd.setEditable(true);
        searchGridPane.add(new Label("名字 "), 0, 0);
        searchGridPane.add(searchNameTextField, 1, 0);
        searchGridPane.add(new Label("年龄 "), 0, 1);
        searchGridPane.add(searchAgeComboBox, 1, 1);
        searchGridPane.add(new Label("类型 "), 0, 2);
        searchGridPane.add(searchTypeComboBox, 1, 2);
        searchGridPane.add(new Label("性别 "), 0, 3);
        searchGridPane.add(unknownButton, 1, 3);
        searchGridPane.add(maleButton, 2, 3);
        searchGridPane.add(femaleButton, 3, 3);
        searchGridPane.add(new Label("创建日期起止 "), 0, 4);
        searchGridPane.add(new Label("更新日期起止 "), 0, 5);
        searchGridPane.add(createDateStart, 1, 4);
        searchGridPane.add(createDateEnd, 2, 4);
        searchGridPane.add(updateDateStart, 1, 5);
        searchGridPane.add(updateDateEnd, 2, 5);
        searchGridPane.add(clearButton, 0, 7);
        searchGridPane.add(searchButton, 1, 7);

        // 折叠面板-搜索
        searchPane = new TitledPane("高级搜索", searchGridPane);
        // 设置外边距，上、右，下，左
        searchPane.setPadding(new Insets(0, 0, 8, 0));
        searchPane.setMaxWidth(width);
        // 设置可折叠
        searchPane.setCollapsible(true);
        // 设置折叠时的动画
        searchPane.setAnimated(true);

        // 注册清空参数事件
        clearButton.setOnAction(event -> clearSearchParam());
    }

    public QueryAnimalRequest genQueryAnimalRequest(int pageSize, int pageNum) {
        String searchNameText = this.getSearchNameTextField().getText();
        Integer searchAgeValue = this.getSearchAgeComboBox().getValue();
        Integer searchTypeValue = this.getSearchTypeComboBox().getValue();
        LocalDate createDateStartValue = this.getCreateDateStart().getValue();
        LocalDate createDateEndValue = this.getCreateDateEnd().getValue();
        LocalDate updateDateStartValue = this.getUpdateDateStart().getValue();
        LocalDate updateDateEndValue = this.getUpdateDateEnd().getValue();
        Object searchSexValue = Objects.nonNull(this.getSexGroup().getSelectedToggle()) ?
                this.getSexGroup().getSelectedToggle().getUserData() : null;

        QueryAnimalRequest queryAnimalRequest = new QueryAnimalRequest();
        queryAnimalRequest.setPageSize(pageSize);
        queryAnimalRequest.setPageNum(pageNum);

        if (StringUtil.isNotEmpty(searchNameText)) {
            queryAnimalRequest.setName(searchNameText);
        }
        if (searchAgeValue != null) {
            queryAnimalRequest.setAge(searchAgeValue);
        }
        if (searchTypeValue != null) {
            queryAnimalRequest.setAnimalTypeCode(searchTypeValue);
        }
        if (searchSexValue != null) {
            queryAnimalRequest.setAnimalSexCode((Integer) searchSexValue);
        }

        // 创建时间起、止选择后生效
        if (Objects.nonNull(createDateStartValue) || Objects.nonNull(createDateEndValue)) {
            queryAnimalRequest.setCreateDateRange(new DateRange(createDateStartValue, createDateEndValue));
        }
        // 更新时间起、止选择后生效
        if (Objects.nonNull(updateDateStartValue) || Objects.nonNull(updateDateEndValue)) {
            queryAnimalRequest.setUpdateDateRange(new DateRange(updateDateStartValue, updateDateEndValue));
        }

        return queryAnimalRequest;
    }

    private void clearSearchParam() {
        searchNameTextField.setText("");
        sexGroup.setUserData(null);
        searchTypeComboBox.setValue(null);
        searchSexComboBox.setValue(null);
        createDateStart.setValue(null);
        createDateEnd.setValue(null);
        updateDateStart.setValue(null);
        updateDateEnd.setValue(null);
    }
}
